﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class OrderDictionary<K,T> where K : IComparable
{
    private List<T> innerList = new List<T>();
    private Dictionary<K, T> innerDict = new Dictionary<K, T>();
    private Comparison<T> comparisonMethod = null;

    public OrderDictionary()
    { 
        
    }

    public OrderDictionary(Comparison<T> comparisonMethod)
    {
        this.comparisonMethod = comparisonMethod;
    }

    public void Add(K key, T value)
    {
        if(innerDict.ContainsKey(key))
        {
            Debug.Log("the key has already exist!");
            return;
        }
        innerDict.Add(key,value);
        innerList.Add(value);
    }

    public void Remove(K key)
    {
        T value;
        if (innerDict.TryGetValue(key, out value))
        {
            innerDict.Remove(key);
            innerList.Remove(value);
        }
    }

    public bool Contains(K key)
    {
        return innerDict.ContainsKey(key);
    }

    public bool ContainsValue(T item)
    {
        return innerList.Contains(item);
    }
    public bool ContainsKey(K key)
    {
        return innerDict.ContainsKey(key);
    }

    public void Clear()
    {
        innerList.Clear();
        innerDict.Clear();
    }

    public void CopyTo(T[] array, int index)
    {
        innerList.CopyTo(array, index);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return innerList.GetEnumerator();
    }

    public int Count
    {
        get
        {
            return innerList.Count;
        }
    }

    public T this[K key]
    {
        get
        {
            return innerDict[key];
        }
        //set
        //{
        //    if (innerDict.ContainsKey(key))
        //    {
        //        innerList[innerList.IndexOf(innerDict[key])] = value;
        //    }
        //    innerDict[key] = value;
        //}
    }

    public void Sort()
    {
        if (comparisonMethod != null)
        {
            innerList.Sort(comparisonMethod);
        }
       
    }
    public void Sort(Comparison<T> comparisonMethod)
    {
        innerList.Sort(comparisonMethod);
    }
}
